#include <iostream>
#include <fstream>
#include <cstring>
#include <map>
#include <vector>
#include <strings.h>
#include <queue>

using namespace std;

class Input_Method//输入法类
{
private:
        map <string,vector <string> > index;//存字库
        int status;//判断输入状态
        int font_line;//判断第几行
        string pinyin_register;//已经输入的拼音
        const int IS_SELECT ;//正在选择文字状态
        const int IS_NOT_SELECT;//未选择文字状态
        queue<string> output;//缓存已经打入的文字
public:
        Input_Method():IS_SELECT(1),IS_NOT_SELECT(2)
        {
            status = IS_NOT_SELECT;
            font_line = 1;
            string filename = "file";
            Add_Font_From_File(filename);
        }
        int Add_Font_From_File(string &filename)//字库导入
        {
                char *buf = new char[2048];
                int len;
                vector <string> font_index;
                ifstream istrm(filename,ios::binary);
                if(!istrm.is_open())
                {
                        cout << "failed to open "<<filename <<endl;
                        throw -1;
                }

                while(1)
                {
                        memset(buf,0,sizeof(char) *2048);
                        istrm.getline(buf,sizeof(int)*2048);
                        string str(buf);
                        len = str.find("=>");

                        string pinyin = str.substr(0,len);
                        string font = str.substr(len +3,str.length()-len-5);

                        vector <string> font_index;
                        for(int i=0;i<(int)font.length()/3;i++)
                        {
                                font_index.push_back(font.substr(i*3,3));
                        }

                        index[pinyin] = font_index;

                        if(istrm.eof())
                                break;
                }
                istrm.close();
                delete []buf;
                return 0 ;
        }
        int Show_One_Font(string &pinyin,int line ,int pos)//打入一个字
        {
            auto search = index.find(pinyin);


            if(search != index.end() )
            {
                auto font_vector = search->second;
                auto it = font_vector.begin() + 5*(line-1) + pos -1;

                cout << "select :" <<*it <<endl;
                output.push(*it);
                return 0;
            }
            else
                return -1;
        }
        int Out_Put_Font()//输出已经打入的字
        {
            if(output.empty())
                return -1;
            while(!output.empty())
            {
                cout << output.front();
                output.pop();
            }
            return 0;
        }
        int Show_One_Line_Font(string &pinyin,int line)//显示一行的字体选择
        {
            auto search = index.find(pinyin);
            int count =0;
            if(search != index.end() )
            {
                auto font_vector = search->second;
                for(auto it = font_vector.begin() + (5*(line-1));it !=font_vector.end();it ++)
                {
                    if(++count == 6)
                        break;
                    cout << count <<" " << *it <<endl;
                }
                return 0;
            }
            else
                return -1;
        }
        int Operator(string &oper)//对外API接口
        {
                if(status == IS_SELECT && oper == "+" )
                {
                    Show_One_Line_Font(pinyin_register,++font_line);
                }
                else if(status == IS_SELECT && oper =="-" && font_line > 1)
                {
                    if(font_line > 1)
                        Show_One_Line_Font(pinyin_register,--font_line);
                    else
                        Show_One_Line_Font(pinyin_register,1);
                }
                else if(status == IS_SELECT && oper == "1")
                {
                    //cout << status;
                    Show_One_Font(pinyin_register,font_line , 1);
                    status = IS_NOT_SELECT;
                }
                else if(status == IS_SELECT && oper == "2")
                {
                    Show_One_Font(pinyin_register,font_line , 2);
                    status = IS_NOT_SELECT;
                }
                else if(status == IS_SELECT && oper == "3")
                {
                    Show_One_Font(pinyin_register,font_line, 3);
                    status = IS_NOT_SELECT;
                }
                else if(status == IS_SELECT && oper == "4")
                {
                    Show_One_Font(pinyin_register,font_line , 4);
                    status = IS_NOT_SELECT;
                }
                else if(status == IS_SELECT && oper == "5")
                {
                    Show_One_Font(pinyin_register,font_line, 5);
                    status = IS_NOT_SELECT;
                }
                else
                {
                    if(Show_One_Line_Font(oper,1) == -1)
                    {
                        status = IS_NOT_SELECT;
                        font_line = 1;
                        return -1;
                    }
                    else
                    {
                        status = IS_SELECT;
                        font_line = 1;
                        pinyin_register = oper;
                        return 0;
                    }
                }
            return 0;

        }
};

int main()
{
    Input_Method InMod;
    int i =0;
    while(++i<10)//自由修改
    {
        string pinyin;
        cin >> pinyin;
        InMod.Operator(pinyin);
    }
    InMod.Out_Put_Font();

}



